/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.mvc.controller;

import java.net.URL;

import com.genia.toolbox.basics.bean.DataContainer;
import com.genia.toolbox.basics.bean.impl.UrlDataContainer;
import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.web.context.WebContext;

/**
 * A controller that allow downloading files from the servlet directory.
 */
public class ServletContentFileController
    extends AbstractDownloadController<Object>
{

  /**
   * constructor.
   */
  public ServletContentFileController()
  {
    super();
    setCaching(true);
  }

  /**
   * the {@link WebContext} to use.
   */
  private WebContext webContext;



  /**
   * returns the {@link DataContainer} to display.
   * 
   * @param form
   *          the form containing the parameter of the request
   * @return the {@link DataContainer} to display or <code>null</code> if no
   *         such object exists
   * @throws TechnicalException
   *           when an error occured
   * @see com.genia.toolbox.web.mvc.controller.AbstractDownloadController#getDataContainer(java.lang.Object)
   */
  @Override
  public DataContainer getDataContainer(Object form)
      throws TechnicalException
  {
    try {
      UrlDataContainer result = new UrlDataContainer();
      final URL url = getServletContext().getResource(getWebContext().getRelativePath());
      if (url == null) {
        return null;
      }
      result.setUrl(url);
      return result;
    }
    catch (Exception e) {
      throw getExceptionManager().convertException(e);
    }
  }



  /**
   * getter for the webContext property.
   * 
   * @return the webContext
   */
  public WebContext getWebContext()
  {
    return webContext;
  }



  /**
   * setter for the webContext property.
   * 
   * @param webContext
   *          the webContext to set
   */
  public void setWebContext(WebContext webContext)
  {
    this.webContext = webContext;
  }

}
